package com.example.scity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.scity.beens.NewsBeen;
import com.example.scity.beens.NewsInfoBeen;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class NewsInfoActivity extends AppCompatActivity {
    NewsInfoBeen newsInfoBeen;
    int id;
    TextView btnPl;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if(msg.what==1){
                TextView txtTitle = findViewById(R.id.newsinfotitle);
                TextView txtContent = findViewById(R.id.newsinfocontent);
                ImageView imageView = findViewById(R.id.newsinfoimageview);
                txtTitle.setText(newsInfoBeen.data.title);
                txtContent.setText(Html.fromHtml(newsInfoBeen.data.content));
                Glide.with(NewsInfoActivity.this).load(Configs.Host+newsInfoBeen.data.cover).into(imageView);
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_info);
        Intent intent = getIntent();
        id = intent.getIntExtra("ID",0);
        loadinfo();

        btnPl = findViewById(R.id.newsinfobutton);
        btnPl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(NewsInfoActivity.this,XinwenPlActivity.class);
                intent1.putExtra("ID",id);
                startActivity(intent1);
            }
        });
    }

    private void loadinfo() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(Configs.Host+ APIInterface.NewsInfo + id).build();//http://124.93.196.45:10001/prod-api/press/press/29
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                newsInfoBeen = new Gson().fromJson(response.body().string(), NewsInfoBeen.class);
                handler.sendEmptyMessage(1);//加载成功
            }
        });
    }
}